package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import org.springframework.validation.annotation.Validated;

/**
 * 护理等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-07
 */
@Service
public class NursingLevelServiceImpl implements INursingLevelService 
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private NursingLevel nursingLevel;
    private static String KEY = "zzyl:nursing:level";


    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectNursingLevelById(id);
    }



    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel) {

        BoundValueOperations<String, String> operations = stringRedisTemplate.boundValueOps(KEY);
// 先从缓存中查询，如果能查到就直接返回，如果查不到就需要从mysql中查询并放入到Redis中
        String redisValue = operations.get();
        if(redisValue == null){
            this.nursingLevel = nursingLevel;
            List<NursingLevelVo> nursingLevelVos = nursingLevelMapper.selectNursingLevelList(nursingLevel);
            operations.set(JSONUtil.toJsonStr(nursingLevelVos));
            return nursingLevelVos;
        }else {
            List<NursingLevelVo> list = JSONUtil.toList(redisValue, NursingLevelVo.class);
            return list;
        }
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        int i = nursingLevelMapper.insertNursingLevel(nursingLevel);
        stringRedisTemplate.delete(KEY);
        return i;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        int i = nursingLevelMapper.updateNursingLevel(nursingLevel);
        stringRedisTemplate.delete(KEY);
        return i;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int i = nursingLevelMapper.deleteNursingLevelByIds(ids);
        stringRedisTemplate.delete(KEY);
        return i;

    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        return nursingLevelMapper.deleteNursingLevelById(id);
    }

    /**
     * 查询所有的护理等级
     *
     * @return
     */

}
